var dbConfig = require('../util/dbconfig');

var moment = require('moment');
//获取分类
var getTally = (req, res) => {
  var sql = 'select * from tally';
  var sqlArr = [];
  var callBack = (err, data) => {
    if (err) {
      console.log('---连接出错了11111----')
    } else {
      res.send(data)
    }
  }
  dbConfig.sqlConnect(sql, sqlArr, callBack);
}
var deleteTally = (req, res) => {
  const id = req.body.id;
  console.log(id)
  var sql = 'DELETE FROM tally WHERE id=?;';
  var sqlArr = [id];
  var callBack = (err, data) => {
    if (err) {
      console.log('---连接出错了11111----')
    } else {
      res.send(data)
    }
  }
  dbConfig.sqldelete(sql, sqlArr, callBack);
}
var addTally = (req, res) => {
  var sqlArr = [];
  const data = req.body.data;
  for (let key in data) {
    sqlArr.push(data[key])
  }
  var formatDate = moment(sqlArr[0]).format('YYYY-MM-DD HH:mm:ss'); /*格式化时间*/

  sqlArr.splice(0, 1, formatDate);

  var sql = "insert into tally(`createdTime`,`type`,`price`,`remark`,`tag`) values(?,?,?,?,?);";

  var callBack = (err, data) => {
    if (err) {
      console.log('---连接出错了11111----')
    } else {
      res.send(data)
    }
  }
  dbConfig.sqladd(sql, sqlArr, callBack);
}

var getPostCate = (req, res) => {
  // let { id } = req.query;
  // var sql = 'select * from post where cate_id=?';
  // var sqlArr = [id];
  // var callBack = (err, data) => {
  //   if (err) {
  //     console.log('---连接出错了2222----')
  //   } else {
  //     res.send({
  //       'list': data
  //     })
  //   }
  // }
  // dbConfig.sqlConnect(sql, sqlArr, callBack);
}

module.exports = {
  getTally,
  deleteTally,
  addTally
};